/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
    if (headA == NULL || headB == NULL)
        return NULL;
    ListNode* ptail1 = headA;
    ListNode* ptail2 = headB;
    int a = 0;
    int b = 0;
    while (ptail1->next != NULL)
    {
        a++;
        ptail1 = ptail1->next;
    }
    while (ptail2->next != NULL)
    {
        b++;
        ptail2 = ptail2->next;
    }
    a++;
    b++;
    if (ptail1 != ptail2)
        return NULL;

    ListNode* phead1 = headA;
    ListNode* phead2 = headB;
    int ret = 0;
    if (a > b)
    {
        ret = a - b;
        while (ret--)
        {
            phead1 = phead1->next;
        }
    }

    if (a < b)
    {
        ret = b - a;
        while (ret--)
        {
            phead2 = phead2->next;
        }
    }

    while (phead1 != NULL)
    {
        if (phead1 == phead2)
            return phead1;
        phead1 = phead1->next;
        phead2 = phead2->next;
    }
    return NULL;


}